package com.atguigu.hellomp;

import com.atguigu.hellomp.entity.User;
import com.atguigu.hellomp.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.function.Consumer;

@SpringBootTest
public class T2 {
    @Autowired
    UserMapper userMapper;

    @Test
    public void test1() {
        //查询name=Tom的所有用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name", "Tom");
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);

    }

    @Test
    public void test2() {
        //查询邮箱域名为baomidou.com的所有用户
        //1111111111
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("email", "baomidou.com");
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);

    }

    @Test
    public void test3() {
        //查询所有用户信息并按照age字段降序排序
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("age");
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);

    }


    @Test
    public void test4() {
        //查询age小于20或大于30的用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lt("age", 20).or().gt("age", 30);
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);
    }

    @Test
    public void test5() {
        //邮箱域名为baomidou.com且年龄小于30或大于40且的用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("email", "baomidou.com")
                .and(q -> q.lt("age", 30).or().gt("age", 40));
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);
    }

    @Test
    public void test6() {
        //邮箱域名为baomidou.com且年龄小于30或大于40且的用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Consumer<QueryWrapper<User>> consumer = q -> q.lt("age", 30).or().gt("age", 40);
        wrapper.like("email", "baomidou.com").and(consumer);
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);
    }

    @Test
    public void test7() {
        //查询age小于20或大于30的用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(User::getAge, 20).or().gt(User::getAge, 30);
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);
    }
}



